//
//  CButton.swift
//  WanZhou
//
//  Created by xhl_dabaoMac on 2021/11/11.
//

import UIKit
import SnapKit
import Kingfisher

///**
///自定义button
///
///
///*/

public class CButton: UIView {

    public var select : Bool  {
        didSet{
            setSelect()
        }
    }
    public var normalTitle : String? = nil
    public var selectTitle : String? = nil
    public var normalImage : UIImage?
    public var selectImage : UIImage?
    public var normalNetImage : String?
    public var selectNetImage : String?

    public var titleNormalColor : UIColor = UIColor.black
    public var titleSelctColor : UIColor = UIColor.red
    
    public let imageView = UIImageView()
    public let titleLabel = UILabel()
    
    public var spaceHeight = 5 {
        didSet{
            updateTitleLabel()
        }
    }

    
    override init(frame: CGRect) {
        select = false
        super.init(frame: frame)
        cinit()
    }
    
    func cinit() {
        self.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(5)
//            make.width.height.equalTo(25)
            make.centerX.equalToSuperview()
        }
        
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: 13)
        
    }
    
    func updateTitleLabel() {
        titleLabel.removeFromSuperview()
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(spaceHeight)
            make.centerX.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
//        debugPrint("deinit:\(self)")
    }
}

public extension CButton{
    
    /// 设置选中
    func setSelect() {
        if select {
            titleLabel.text = selectTitle ?? normalTitle
            titleLabel.textColor = titleSelctColor
            guard let url = selectNetImage else {
                imageView.image = selectImage
                return
            }
            //设置网络图
            imageView.kf.setImage(with: URL(string: url))
            
        }else{
            titleLabel.text = normalTitle
            titleLabel.textColor = titleNormalColor

            guard let url = normalNetImage else {
                imageView.image = normalImage
                return
            }
            //设置网络图
            imageView.kf.setImage(with: URL(string: url))
        }
    }
}

///提供几种批量初始化的方法
public extension CButton{
    
    /// 批量初始化
    /// - Parameters:
    ///   - titles: 标题
    ///   - normalImage: 正常状态图片
    ///   - selectImages: 选中状态的图片
    ///   - spaceHeight: 标题和图片的间隔
    ///   - titleNormalColor: 标题颜色
    /// - Returns: 装有CButton的数组
    static func getCButtons(titles:[String],
                            normalImage:[UIImage?],
                            selectImages:[UIImage?]? = nil,
                            spaceHeight:Int = 5,
                            titleNormalColor: UIColor = CColor.c33) -> [CButton] {
        
        var btns = [CButton]()
        if titles.count != normalImage.count { return btns}
        for i in 0 ..< titles.count {
            let cbutton = CButton()
            cbutton.normalTitle = titles[i]
            cbutton.normalImage = normalImage[i]
            cbutton.spaceHeight = spaceHeight
            if let slims = selectImages {
                cbutton.selectImage = slims[i]
            }
            cbutton.titleNormalColor = titleNormalColor
            cbutton.select = false
            btns.append(cbutton)
        }
        return btns
    }
    
    static func getOneCButton(title:String,
                              normalImage:UIImage?,
                              selectImage:UIImage? = nil,
                              spaceHeight:Int = 5,
                              titleNormalColor: UIColor = CColor.c33) -> CButton {
        
        let btn : CButton = getCButtons(titles: [title],
                                           normalImage: [normalImage],
                                           selectImages: [selectImage],
                                           spaceHeight: spaceHeight,
                                           titleNormalColor: titleNormalColor).first!
        
        return btn
    }
}
